package com.jeomo.masterdata.provider.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.masterdata.provider.request.CreateGoodsRequest;
import com.jeomo.masterdata.provider.request.UpdateGoodsRequest;
import com.jeomo.masterdata.provider.web.vo.GoodsVo;
import com.jeomo.masterdata.starter.entity.Goods;
import com.jeomo.masterdata.starter.exception.IntegrityConstraintViolationException;
import com.jeomo.masterdata.starter.service.IGoodsService;
import com.jeomo.shiro.util.LoginSessionUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * <h3>jeomo</h3>
 * <p>商品controller</p>
 *
 * @author : 崔贺
 * @date : 2020-08-28 13:56
 **/
@RequestMapping("/masterData/goods")
@RestController
public class GoodsController extends BaseController {

    @Resource
    private IGoodsService goodsService;

    @PostMapping
    @ApiOperation("创建商品")
    public CommonResult<String> create(@RequestBody CreateGoodsRequest createGoodsRequest) {

        try {
            Goods goods = BeanCopyUtil.copyProperties(createGoodsRequest, Goods.class);
            goodsService.create(goods);
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new IntegrityConstraintViolationException();
            } else {
                e.printStackTrace();
                return CommonResult.failure("新增失败");
            }
        }
        return CommonResult.success("新增成功");

    }

    @DeleteMapping("/{code}")
    @ApiOperation("删除商品")
    public CommonResult<Boolean> remove(@PathVariable("code") String code) {
        return CommonResult.success(goodsService.removeById(code));
    }

    @PutMapping
    @ApiOperation("修改商品")
    public CommonResult<String> update(@RequestBody UpdateGoodsRequest updateGoodsRequest) {
        try {
            Goods goods = BeanCopyUtil.copyProperties(updateGoodsRequest, Goods.class);
            goodsService.updateById(goods);
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new IntegrityConstraintViolationException();
            } else {
                e.printStackTrace();
                return CommonResult.failure("修改失败");
            }
        }
        return CommonResult.success("修改成功");

    }

    @PostMapping("/page")
    @ApiOperation("获取商品列表")
    public CommonResult<PageResponseVo<GoodsVo>> page(@RequestBody PageRequest request) {
        PageResponseDto pageResponseDto = goodsService.pageSearch(BeanCopyUtil.copyProperties(request, PageQueryDto.class));
        return CommonResult.success(BeanCopyUtil.copyPageProperties(pageResponseDto, GoodsVo.class));
    }

    @GetMapping("/{goodsCode}")
    @ApiOperation("根据商品号查询商品实体")
    public CommonResult<GoodsVo> queryByCode(@PathVariable("goodsCode") String goodsCode) {
        Goods goods = goodsService.getById(goodsCode);
        return CommonResult.success(BeanCopyUtil.copyProperties(goods, GoodsVo.class));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有商品")
    public CommonResult<List<GoodsVo>> all() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("start_flag", true);
        wrapper.eq("org_code", LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode());
        List<Goods> goods = goodsService.list(wrapper);
        return CommonResult.success(BeanCopyUtil.copyListProperties(goods, GoodsVo::new));
    }

    @PostMapping("/selectGoodsByGoodsTypeCodes")
    @ApiOperation("根据品类code列表获取商品列表(也可以传入单个GoodsTypeCodes进行查询)")
    public CommonResult<List<GoodsVo>> selectGoodsByGoodsTypeCodes(@RequestBody List<String> goodsTypeCodes) {
        List<Goods> goods = goodsService.selectGoodsByGoodsTypeCodes(goodsTypeCodes);
        return CommonResult.success(BeanCopyUtil.copyListProperties(goods, GoodsVo::new));
    }

    @PostMapping("/selectGoodsByTobCodes")
    @ApiOperation("根据品类code列表获取商品列表(也可以传入单个tobCodes进行查询)")
    public CommonResult<List<GoodsVo>> selectGoodsByTobCodes(@RequestBody List<String> tobCodes) {
        List<Goods> goods = goodsService.selectGoodsByTobCodes(tobCodes);
        return CommonResult.success(BeanCopyUtil.copyListProperties(goods, GoodsVo::new));
    }
}
